﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DienThoai.MODELS
{
    class ManagerModel
    {
        public delegate void DeletePhoneEventHandler(object sender, EventArgs e);
        public event DeletePhoneEventHandler DeletePhoneEvent;
        public delegate void DeleteAccessoryEventHandler(object sender, EventArgs e);
        public event DeleteAccessoryEventHandler DeleteAccessoryEvent;
        
        private DienThoaiDataContext db = new DienThoaiDataContext();
        
        private ManagerModel() { }

        private static ManagerModel instance;

        public static ManagerModel Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ManagerModel();
                }
                return instance;
            }
        }

        /// <summary>
        /// This method is used to delete phone
        /// </summary>
        /// <param name="phone">The phone to be deleted</param>
        public void DeletePhone(Phone phone)
        {
            var deletedProduct = from i in db.Products
                                 where i.ProductID == phone.PhoneID
                                 select i;
            var deletedPhone = from i in db.Phones
                               where i.PhoneID == phone.PhoneID
                               select i;
            foreach (var i in deletedPhone)
                db.Phones.DeleteOnSubmit(i);
            foreach (var i in deletedProduct)
                db.Products.DeleteOnSubmit(i);
            db.SubmitChanges();
            OnDeletePhoneCompleted(new EventArgs());
        }

        public void DeleteAccessory(Accessory accessory)
        {
            var deletedProduct = from i in db.Products
                                 where i.ProductID == accessory.AccessoryID
                                 select i;
            var deletedAccessory = from i in db.Accessories
                                   where i.AccessoryID == accessory.AccessoryID
                                   select i;
            foreach (var i in deletedAccessory)
                db.Accessories.DeleteOnSubmit(i);
            foreach (var i in deletedProduct)
                db.Products.DeleteOnSubmit(i);
            db.SubmitChanges();
            OnDeleteAccessoryCompleted(new EventArgs());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        private void OnDeletePhoneCompleted(EventArgs e)
        {
            if (DeletePhoneEvent != null)
            {
                DeletePhoneEvent(this, e);
            }
        }

        private void OnDeleteAccessoryCompleted(EventArgs e)
        {
            if (DeleteAccessoryEvent != null)
            {
                DeleteAccessoryEvent(this, e);
            }
        }
    }
}
